package com.easysoft.framework.action;

import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.menu.service.SysMenuService;
import com.easysoft.framework.role.model.RoleMenu;
import com.easysoft.framework.role.model.RoleMenuView;
import com.easysoft.framework.role.model.SysRoleView;
import com.easysoft.framework.role.service.RoleMenuService;
import com.easysoft.framework.role.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by try on 2017/7/12.
 */
@RestController
@RequestMapping(value = SystemRoleMenuAction.BASE_VIEW)
public class SystemRoleMenuAction {
        public static final String BASE_VIEW = "/role";
        private final static Logger logger = LoggerFactory.getLogger(SystemRoleMenuAction.class);

        @Autowired
        @Qualifier("service.SysMenuService")
        private SysMenuService sysMenuService;

        @Autowired
        @Qualifier("service.SysRoleService")
        private RoleService roleService;


        @Autowired
        @Qualifier("service.RoleMenuService")
        private RoleMenuService roleMenuService;
        /**
         * 角色与菜单关系列表
         *
         * @return
         */
        @RequestMapping(value = "/listRoleAndMenu")
        public ModelAndView listRoleAndMenu() {

            logger.info("Call SysRoleMenuAction.listRoleAndMenu()");
            //
            ////查询启用的菜单
            List<Map<String, Object>> listMenu = sysMenuService.getSysMenuTree(null);
            ////查询启用的角色
            List<SysRoleView> listRole = roleService.selectDisableRole(0);
            //
            ModelAndView mv = new ModelAndView(BASE_VIEW + "/listRoleAndMenu");
            mv.addObject("listMenu", listMenu); // 返回查询结果集
            mv.addObject("listRole", listRole); // 返回查询结果集
            return mv;
        }

        /**
         * 根据角色查询菜单
         * @param roleId
         * @return
         */
        @RequestMapping(value = "/selectMenuByRoleId", method = RequestMethod.POST)
        public List<RoleMenuView> selectMenuByRoleId(String roleId) {

            logger.info("Call SysRoleMenuAction.selectMenuByRoleId({})",roleId);

            return roleMenuService.selectRoleMenuRelByRoleId(roleId);

        }

        /**
         * 保存菜单和角色关系
         * @param roleMenu
         * @param session
         * @return
         */
        @RequestMapping(value = "/saveRoleAndMenuRel", method = RequestMethod.POST)
        @SystemControllerLog(description = "保存菜单和角色关系")
        public String saveRoleAndMenuRel(RoleMenu roleMenu, HttpSession session) {

            String userId = (String) session.getAttribute(WebConstants.SESSION_USER_ID);

            logger.info("Call SysRoleMenuAction.saveRoleAndMenuRel({}, {})", roleMenu.toString(), userId);

            return roleMenuService.saveRoleMenu(roleMenu);
        }

        /**
         * 批量保存菜单和角色关系
         * @param roleMenuRelstr
         * @param session
         * @return
         */
        @RequestMapping(value = "/saveList", method = RequestMethod.POST)
        public int saveList(String roleMenuRelstr, HttpSession session,String roleId) {

            String userId = (String) session.getAttribute(WebConstants.SESSION_USER_ID);

            logger.info("Call SysRoleMenuAction.saveList({}, {})", roleMenuRelstr, userId);
            int result = roleMenuService.saveList(roleMenuRelstr, userId, roleId);
            return result;
        }
}


